<template>
	<view class="link">
		<view class="colleagues-name">
			企业名称：
			<view class="uni-input colleagues-name">
				{{companyname}}
			</view>
		</view>
		<view class="colleagues-name" v-if="uinfo.roleid!='2'||departname">
			企业部门：
			<view class="uni-input colleagues-name" >
				{{departname}}
			</view>
		</view>
		<view class="colleagues-name f14 border" v-if="uinfo.roleid=='2'">
			部门名称：
			<view class="uni-input colleagues-name" @tap="toggleTab('selector')">
				<view v-if="deptlist!=0" class="tab" v-bind:class="{ active: isActive }" >{{ resultInfodept.result?resultInfodept.result:'请选择加入部门'}}</view>
				<m-input @click="inputstate" v-if="deptlist.length==0" type="text" class="f14" readonly clearable v-model="address"  placeholder="请选择加入部门"></m-input>				
				<view class="" style="position: absolute;right: 10px;">
					<uni-icons type="arrowright" size="20"></uni-icons>
				</view>
			</view>
			
		</view>
		<view class="colleagues-name" >
			职位名称：{{uinfo.roleid=='2'?'部门经理':'普通员工'}}			
		</view>
		<view class="colleagues-name">
			用户姓名：
			<view class="uni-input colleagues-name">
				{{shareusername}}
			</view>
			
		</view>
		<view class="colleagues-name" >
			审核理由：退出公司	
		</view>
		<!-- <view class="position">
			职位：
			<input class="uni-input position" name="input" disabled placeholder="请填写部门名称" />
		</view> -->
		<view class="bnt-all" v-if="showbackindex">
			<view class="flex minbtn">
				<view class="bnt-add2 w50" @click="derefuse">不同意</view>
				<view class="bnt-add w50 mL10" @click="adduser">同意</view>
			</view>
		</view>
		<w-picker
		    :visible.sync="date3visible"
		    mode="selector"
		    value=""
		    default-type="name"
		    :default-props="defaultProps"
		    :options="deptlist"
		    @confirm="onConfirm($event,'selector')"
		    @cancel="onCancel"
		    ref="selector" 
		></w-picker>
		<!-- <backhome v-if="!showbackindex"></backhome> -->
		<!-- 底部导航 -->
		<footernav pagePath='user' v-if="!showbackindex"></footernav>
		<!-- 加载动画 -->
		<loading v-if="!isloading()"></loading>
	</view>
</template>

<script>
	import {deptuseradd,deptinfo,applystate} from '../../common/api/department.js'; //引进requst方法
	import wPicker from '@/components/w-picker/w-picker.vue';
	import backhome from '@/components/backhome.vue';
	import {questiontrain} from '@/common/api/topics.js'	
	import {userinfo} from '@/common/api/user.js'
	import {showgetlist} from '@/common/api/department.js'
	
	import {deptjrefuse} from '@/common/api/company.js';
	
	export default {
		components: {
			backhome
		},
		data() {
			return {
				showbackindex:true,
				date3visible:false,
				defaultProps:{
					label:"name",
					value:"id"
				},
				resultInfodept:{
					result:'',
				},	
				companyid:"",
				detaildata:[],
				id:"",
				departname:"",
				companyname:"",
				uinfo:'',
				deptlist:[],
				shareuserid:"",
				shareusername:"",
				roleid:"",
				page:1,
				address:"请选择加入部门",
				pagesize:"10",
				deptid:"",
				shareapplyid:'',
			};
			
		},
		methods: {
			toggleTab(){
				this.date3visible=true;
				
			},
			getdeptlist(){
				showgetlist({
					companyid:this.companyid,
					page:this.page,
					pagesize:this.pagesize,
					type:'',
				}).then(res=>{
					this.deptlist = res.data.data
					
				})
				console.log(this.deptlist)
			},
			onCancel(res,val){
			/*业务逻辑*/
			},
			onConfirm(res,val) {
				// console.log(res)
				// console.log(val);
				//如果页面需要调用多个mode类型，可以根据mode处理结果渲染到哪里;
				switch (val) {					
					case 'selector':
						this.resultInfodept.result = res.result
						this.address = res.result
						this.deptid =res.value
						break;
				}
				// this.resultInfo = { ...val };
				// this.isActive = false;
			},
			derefuse(){
				let that=this;
				uni.showModal({
				    title: '确定审核不通过?',
				    success: (res) => {
				    	if (res.confirm) {
							
				applystate({applyid:that.shareapplyid,companyid:that.companyid,uid:that.shareuserid,status:'2'}).then(res=>{
					console.log(res)
					uni.showModal({
					  content: res.data.message,
					  showCancel: false,
					  buttonText: '我知道了',
					  success: (res) => {
						if (res.confirm) {
							uni.reLaunch({
								url: '/pages/user/user'
							});
						} else if (res.cancel) {
						  console.log('用户点击取消');
						}
					  }
					})
				})
				} else if (res.cancel) {
				  console.log('用户点击取消');
				}
			  }
			})
			},
			adduser(){
				let that=this;
				uni.showModal({
				    title: '确定审核通过?',
				    success: (res) => {
				    	if (res.confirm) {
				    		if(that.companyid != that.uinfo.companyid && that.uinfo.company_name!=null){
				    			
				    			uni.showModal({
				    			  content: '您当前您隶属于'+that.uinfo.company_name+',无法加入其它公司部门',
				    			  showCancel: false,
				    			  buttonText: '我知道了',
				    			  success: (res) => {
				    				if (res.confirm) {
				    				
				    				} else if (res.cancel) {
				    				  console.log('用户点击取消');
				    				}
				    			  }
				    			})
				    			that.showbackindex=false;
				    			return;
				    		}
				    		// if(that.uinfo.roleid==2){			
				    		
				    		// 	if(!that.deptid){
				    		// 		uni.showToast({
				    		// 			title: '请选择要加入的部门',
				    		// 			duration: 3000,
				    		// 			icon:'none'
				    		// 		});
				    		// 		return;
				    		// 	}
				    		// 	var deptid=that.deptid;
				    		// }else{
				    		// 	var deptid=that.uinfo.deptid
				    		// }
				    		// let userid=uni.getStorageSync('userid');
				    		let userid = that.uinfo.id; 
				    		
				    		applystate({applyid:that.shareapplyid,uid:that.shareuserid,companyid:that.companyid,status:'1'}).then(res=>{
				    			console.log(res)
				    			that.showbackindex=false;
				    			if(res.data.code == 200){
				    				
									uni.showModal({
									  content: res.data.message,
									  showCancel: false,
									  buttonText: '我知道了',
									  success: (res) => {
										if (res.confirm) {
											uni.reLaunch({
											    url: '/pages/user/user'
											});
										} else if (res.cancel) {
										  console.log('用户点击取消');
										}
									  }
									})
									
				    			}else{
									
									uni.showModal({
									  content: res.data.message,
									  showCancel: false,
									  buttonText: '我知道了',
									  success: (res) => {
										if (res.confirm) {
											
										} else if (res.cancel) {
										  console.log('用户点击取消');
										}
									  }
									})
				    			}
				    		})
				    	} else if (res.cancel) {
				    	  console.log('用户点击取消');
				    	}
				      }
				    })
			},
		},
		onLoad(event) {
			try {
				this.detaildata = JSON.parse(decodeURIComponent(event.details));
				console.log(this.detaildata);
			} catch (error) {
				this.detaildata = JSON.parse(event.details);
				console.log(this.detaildata);
			}
			this.id = this.detaildata.companyid;
			this.shareapplyid= this.detaildata.applyid;
			this.shareuserid=this.detaildata.shareuserid;		
			this.shareusername=this.detaildata.shareusername;	
			// this.id = this.detaildata.deptid;
			// this.is_m=this.detaildata.is_m;
			// identity_state  用户状态0-正常 1-退出公司中 2-退出部门中 3-禁用(整系统)
			userinfo({uid:this.shareuserid}).then(res=>{
				this.uinfo = res.data.data
				uni.setStorage({
					key: 'userInfo',
					data: res.data
				});
				this.companyname=res.data.data.company_name;	
				this.departname=res.data.data.dept_name;
				this.companyid =res.data.data.companyid;
				if(res.data.data.companyid!=this.id){
					// uni.showToast({
					//     title:  '授权公司不对，请检查！',
					//     duration: 2000,
					// 	icon:'none'
					// });
					uni.showModal({
					  content: '授权公司不对，请检查！',
					  showCancel: false,
					  buttonText: '我知道了',
					  success: (res) => {
						if (res.confirm) {
							// setTimeout(function() {
								uni.reLaunch({
									url: '/pages/user/user'
								});
							// },5000);
						} else if (res.cancel) {
						  console.log('用户点击取消');
						}
					  }
					})
					
					
					return;
				}
				if(!res.data.data.companyid){
					// uni.showToast({
					//     title:  '授权公司不对，请检查！',
					//     duration: 2000,
					// 	icon:'none'
					// });
					// setTimeout(function() {
					// 	uni.reLaunch({
					// 	    url: '/pages/user/user'
					// 	});
					// },5000);
					
					uni.showModal({
					  content: '授权公司不对，请检查！',
					  showCancel: false,
					  buttonText: '我知道了',
					  success: (res) => {
						if (res.confirm) {
							// setTimeout(function() {
								uni.reLaunch({
									url: '/pages/user/user'
								});
							// },5000);
						} else if (res.cancel) {
						  console.log('用户点击取消');
						}
					  }
					})
					
					
					return;
				}
			});
			this.getdeptlist();
			// return
		},
	}
</script>

<style lang="scss">
.link {
	width: 100%;
	background-color: white;
	.colleagues-name {
		color: rgba(80, 80, 80, 1);
		font-size: 15px;
		display: flex;
		align-items: center;
		width: 95%;
		margin: auto;
		padding: 10px 0;
		border-bottom: 1px solid #eee;
	}
	.position {
		color: rgba(80, 80, 80, 1);
		font-size: 15px;
		display: flex;
		align-items: center;
		width: 95%;
		margin: auto;
		padding: 5px 0;
		border-bottom: 1px solid #eee;
	}
	.uni-input {
		border: none;
	}
	.bnt-all{
		width: 100%;
		height: 42px;
		
		border-radius: 18px;
		font-size: 15px;
		line-height: 42px;
		text-align: center;
		margin: auto;
		position: absolute;
		bottom: 50px;
		left: 0%;	
	}
	.bnt-all .minbtn{width: 70%; margin: 0 auto;}
	.bnt-add {
		width: 192px;
		height: 42px;
		color: rgba(255, 255, 255, 1);
		background-color: rgba(42, 130, 228, 1);
		border-radius: 18px;
		font-size: 15px;
		line-height: 42px;
		text-align: center;
	}
	
	.bnt-add2 {
		width: 192px;
		height: 42px;
		color: rgba(255, 255, 255, 1);
		background-color: red;
		border-radius: 18px;
		font-size: 15px;
		line-height: 42px;
		text-align: center;
	}
}
</style>